﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StudentInfoSystem.Data;

namespace StudentInfoSystem.View
{
    public partial class MainForm : Form
    {
        private Data.UserInfo currentUser = new Data.UserInfo();
        private Student currentStudent;

        public Student CurrentStudent
        {
            get { return currentStudent; }
            set
            {
                currentStudent = value;
                if (currentStudent != null)
                {
                    DisplayStudentInfo(currentStudent);
                }
            }
        }

        public MainForm()
        {
            InitializeComponent();

            RefreshRights();
        }

        public void ClearForm()
        {
            this.textBoxFirstName.Clear();
            this.textBoxMiddleName.Clear();
            this.textBoxLastName.Clear();
            this.textBoxFacultyNumber.Clear();
            this.textBoxMajor.Clear();
            this.textBoxFaculty.Clear();
            this.comboBoxStatus.SelectedIndex = -1;
            this.comboBoxOKS.SelectedIndex = -1;
            this.numericUpDownCourseNumber.Value = 0;
            this.textBoxGroup.Clear();
            this.textBoxStream.Clear();
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            switch (currentUser.UserRole)
            {
                case Data.UserRole.Anonymous:
                    {
                        LoginUser();
                        this.loginToolStripMenuItem.Text = "Logout";
                        break;
                    }
                case Data.UserRole.Student:
                    {
                        Logout();
                        this.loginToolStripMenuItem.Text = "Login";
                        break;
                    }
                case Data.UserRole.Admin:
                    {
                        Logout();
                        this.loginToolStripMenuItem.Text = "Login";
                        break;
                    }
            }
        }

        private void Logout()
        {
            this.currentUser = new Data.UserInfo();
            DisableFormControls();
            ClearForm();
        }

        private void LoginUser()
        {
            LoginForm loginForm = new LoginForm();

            string userName = string.Empty;
            bool isLoginSuccressfull = loginForm.TryLogin(out userName);

            if (isLoginSuccressfull)
            {
                //TO DO: do something
                MessageBox.Show("Login successfull");
                currentUser.UserName = userName;
                var user = UserDataAccess.GetUser(currentUser.UserName);
                currentUser.User = user;
                switch (user.Role)
                {
                    case "Student":
                        {
                            currentUser.UserRole = Data.UserRole.Student;
                            CurrentStudent = StudentDataAccess.GetStudentByUserId(user.ID);
                            DisplayStudentInfo(CurrentStudent);
                            break;
                        }
                    case "Teacher":
                        {
                            break;
                        }

                    case "Admin":
                        {
                            currentUser.UserRole = Data.UserRole.Admin;
                            buttonNewStudent.Visible = true;
                            break;
                        }
                    default:
                        { break; }
                }
                EnableFormControls();
                RefreshRights();
            }
            else
            {
                //TO DO: do something if login is unsucvvessfull
                MessageBox.Show("Login Unsuccessfull");
            }
        }

        private void buttonNewStudent_Click(object sender, EventArgs e)
        {
            AddNewStudentForm addStudent = new AddNewStudentForm();
            addStudent.Show();
        }

        public void EnableFormControls()
        {
            ChangeFormControlsState(true);
        }

        public void DisableFormControls()
        {
            ChangeFormControlsState(false);
        }

        public void ChangeFormControlsState(bool isEnabled)
        {
            this.viewStudentsToolStripMenuItem.Visible = isEnabled;
            this.currentStudentInfoTextToolStripMenuItem.Visible = isEnabled;
            this.textBoxFirstName.Enabled = isEnabled;
            this.textBoxMiddleName.Enabled = isEnabled;
            this.textBoxLastName.Enabled = isEnabled;
            this.textBoxFacultyNumber.Enabled = isEnabled;
            this.textBoxMajor.Enabled = isEnabled;
            this.textBoxFaculty.Enabled = isEnabled;
            this.comboBoxStatus.Enabled = isEnabled;
            this.comboBoxOKS.Enabled = isEnabled;
            this.numericUpDownCourseNumber.Enabled = isEnabled;
            this.textBoxGroup.Enabled = isEnabled;
            this.textBoxStream.Enabled = isEnabled;
            this.buttonBrowsePicture.Enabled = isEnabled;
            this.buttonNewStudent.Visible = false;
        }

        public void RefreshRights()
        {
            if (currentUser.User == null)
            {
                DisableFormControls();
                buttonNewStudent.Visible = false;
                return;
            }

            if (currentUser.UserRole == Data.UserRole.Student)
            {
                EnableFormControls();
                buttonNewStudent.Visible = false;
            }
            else
                if (currentUser.UserRole == Data.UserRole.Admin)
                {
                    this.buttonNewStudent.Visible = true;
                    DisableFormControls();
                    buttonNewStudent.Visible = true;
                }
        }

        public void DisplayStudentInfo(Student student)
        {
            if (student == null)
            {
                MessageBox.Show("there is no info for this student");
                return;
            }

            textBoxFirstName.Text = student.FirstName;
            textBoxMiddleName.Text = student.MiddleName;
            textBoxLastName.Text = student.LastName;
            textBoxFaculty.Text = student.Faculty;
            textBoxMajor.Text = student.Major;

            switch (student.OKS)
            {
                case 1: comboBoxOKS.SelectedIndex = 1; break;
                case 2: comboBoxOKS.SelectedIndex = 2; break;
                case 3: comboBoxOKS.SelectedIndex = 3; break;
                default: comboBoxOKS.SelectedIndex = 0; break;
            }

            switch (student.StudentStatus)
            {
                case 1: comboBoxStatus.SelectedIndex = 1; break;
                case 2: comboBoxStatus.SelectedIndex = 2; break;
                case 3: comboBoxStatus.SelectedIndex = 3; break;
                default: comboBoxStatus.SelectedIndex = 0; break;
            }

            textBoxFacultyNumber.Text = student.FacultyNumber;
            textBoxStream.Text = student.Stream;
            numericUpDownCourseNumber.Value = student.Course;
            textBoxGroup.Text = student.Group.ToString();
        }

        public void PopulateStudentData(ref Student student)
        {
            if (student == null)
            {
                throw new ArgumentException("student parameter must not be null");
            }

            student.FirstName = textBoxFirstName.Text;
            student.MiddleName = textBoxMiddleName.Text;
            student.LastName = textBoxLastName.Text;
            student.Faculty = textBoxFaculty.Text;
            student.Major = textBoxMajor.Text;

            switch (comboBoxOKS.SelectedIndex)
            {
                case 1: student.OKS = 1; break;
                case 2: student.OKS = 2; break;
                case 3: student.OKS = 3; break;
                default: student.OKS = 0; break;
            }

            switch (comboBoxStatus.SelectedIndex)
            {
                case 1: student.StudentStatus = 1; break;
                case 2: student.StudentStatus = 2; break;
                case 3: student.StudentStatus = 3; break;
                default: student.StudentStatus = 0; break;
            }

            student.FacultyNumber = textBoxFacultyNumber.Text;
            student.Stream = textBoxStream.Text;
            student.Group = (int)numericUpDownCourseNumber.Value;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void buttonBrowsePicture_Click(object sender, EventArgs e)
        {
            DialogResult pictureDialogResult = this.openPictureDialog.ShowDialog();
            if (pictureDialogResult == DialogResult.OK)
            {
                this.pictureBoxStudentPhoto.ImageLocation = this.openPictureDialog.FileName;
            }
        }

        public string GenerateInfoText()
        {
            string infoText = "Уверение\n";
            infoText += this.textBoxFirstName.Text + " " + this.textBoxMiddleName.Text + " " +
            this.textBoxLastName.Text;
            infoText += " е студент/ка във Факултет " + this.textBoxFaculty.Text +
            ", специалност " + this.textBoxMajor.Text;
            infoText += "\n";
            return infoText;
        }

        private void currentStudentInfoTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string userInfo = GenerateInfoText();
            InfoForm infoForm = new InfoForm();
            infoForm.Controls["richTextBoxInfo"].Text = GenerateInfoText();
            infoForm.Show();
        }

        private void viewStudentsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StudentQueryForm queryForm = new StudentQueryForm();
            queryForm.Show();
        }

        //private void registerToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    RegisterForm newRegistration = new RegisterForm();
        //    newRegistration.Show();
        //}
    }
}
